package com.hc.springbootzookeeper.service.impl;


import com.hc.springbootzookeeper.entity.Order;
import com.hc.springbootzookeeper.entity.Product;
import com.hc.springbootzookeeper.mapper.OrderMapper;
import com.hc.springbootzookeeper.mapper.ProductMapper;
import com.hc.springbootzookeeper.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public void reduceStock(int id) throws Exception{
        Product product = productMapper.getProduct(id);
        if(product.getStock() > 0){
            int i = productMapper.reduceStock(id);
            if(i == 1){
                Order order = new Order();
                order.setPid(id);
                order.setNumber(UUID.randomUUID().toString());
                orderMapper.insert(order);
            }else {
                throw new NullPointerException("秒杀失败");
            }
        }else {
            throw new NullPointerException("已无库存");
        }
    }
}
